﻿using Domain.ValueObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Entity
{
    public record User : IMyRoot
    {
        private User() { }

        public User(PhoneNumber phone)
        {
            this.Id = Guid.NewGuid();
            this.Phone = phone;
            AccessFail = new UserAccessFail(this);
        }

        public Guid Id { get; init; }
        public PhoneNumber Phone { get; private set; }

        private string? passwordHash;
        public UserAccessFail AccessFail { get; private set; }

        /// <summary>
        /// 是否设置了密码
        /// </summary>
        /// <returns></returns>
        public bool HasPassword()
        {
            return !string.IsNullOrEmpty(passwordHash);
        }
        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="pwd"></param>
        public void ChangePassword(string pwd)
        {
            if (pwd.Length <= 3) throw new ArgumentException("密码长度不能小于3");
            passwordHash = MyHelper.EncryptString(pwd);
        }

        /// <summary>
        /// 验证密码是否正确
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckPassword(string password) 
        {
            return passwordHash.Equals(MyHelper.EncryptString(password));
        }

        /// <summary>
        /// 更改手机号
        /// </summary>
        /// <param name="phone"></param>
        public void ChangePhone(PhoneNumber phone)
        {
            this.Phone = phone;
        }

    }
}
